package com.tt.job.zookeeper;

import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.framework.recipes.leader.LeaderLatch;
import org.apache.curator.retry.ExponentialBackoffRetry;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

/**
 * Zookeeper 工厂类
 */
@Component
public class ZookeeperBeanFactory {

    @Autowired
    private ZookeeperSettings settings;

    @Bean
    public LeaderLatch leaderLatch(CuratorFramework client) {
        String path = settings.getRoot() + settings.getLeaderPath();
        return new LeaderLatch(client, path);
    }

    @Bean
    public CuratorFramework zookeeperClient() {
        return CuratorFrameworkFactory.newClient(settings.getHost(), new ExponentialBackoffRetry(1000, 3));
    }
}
